package com.itfreer.form;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.entity.metadata.MetadataDirectoryEntity;
import com.itfreer.utils.HeaderUtils;
import com.itfreer.utils.json.JsonUtils;
import com.itfreer.utils.sys.SystemParameter;

/**
 * 元数据名称rest访问客户端
 */
@Component("MetadataDirectoryRestClient")
public class MetadataDirectoryRestClient {

	private final String getEntitysUrl = "/metadatadirectory/getEntitys";
	private final String getRowCountsUrl = "/metadatadirectory/getRowCounts";
	private final String getEntityUrl = "/metadatadirectory/getEntity/";

	@Autowired
	private SystemParameter systemParameter;
	
	/**
	 * 获取实体列表
	 * 
	 * @param queryFields
	 *            查询字段
	 * @param where
	 *            查询条件
	 * @param order
	 *            排序条件
	 * @param pageSize
	 *            分页大小
	 * @param pageIndex
	 *            分页索引
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public List<MetadataDirectoryEntity> getEntitys(String queryFields, Map<String, Object> where, Map<String, Integer> order,
			Integer pageSize, Integer pageIndex) throws IOException {
		HashMap<String, Object> args = new HashMap<String, Object>();
		args.put("queryFields", queryFields);
		if (where != null) {
			args.put("where", JsonUtils.toJSONString(where));
		}
		if (order != null) {
			args.put("order", JsonUtils.toJSONString(order));
		}
		args.put("pageSize", pageSize);
		args.put("pageIndex", pageIndex);
		String argsString = JsonUtils.toJSONString(args);

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getEntitysUrl);
		Response response = target.request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(Entity.json(argsString)).invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			List<MetadataDirectoryEntity> rList = (List<MetadataDirectoryEntity>) JsonUtils.toBean(resultJson,
					JsonUtils.getCollectionType(List.class, MetadataDirectoryEntity.class));
			return rList;
		}
		return null;
	}

	/**
	 * 获取当前条件下的数据行总数
	 * 
	 * @param where
	 *            查询条件
	 * @return
	 * @throws IOException
	 */
	public Long getRowCounts(Map<String, Object> where) {
		HashMap<String, Object> args = new HashMap<String, Object>();
		if (where != null) {
			args.put("where", JsonUtils.toJSONString(where));
		}
		String argsString = JsonUtils.toJSONString(args);

		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getRowCountsUrl);
		Response response = target.request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(Entity.json(argsString)).invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return 0L;
			}
			return Long.parseLong(resultJson);
		}
		return 0L;
	}

	/**
	 * 获取实体
	 * 
	 * @param guid
	 *            唯一值
	 * @return
	 * @throws IOException
	 */
	public MetadataDirectoryEntity getEntity(String guid) throws IOException {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(systemParameter.getRemoteUrl() + getEntityUrl + guid);
		Response response = target.request()
				.headers(HeaderUtils.getSysHeader(systemParameter.getsId(), systemParameter.getsPwd()))
				.buildPost(null).invoke();
		if (response.getStatus() == 200) {
			String resultJson = response.readEntity(String.class);
			if (resultJson == null || resultJson.equals("")) {
				return null;
			}
			return JsonUtils.toBean(resultJson, MetadataDirectoryEntity.class);
		}
		return null;
	}
}
